import { useUserStoreWidthOut } from '@/stores/user';

export function usePermission() {
  const userStore = useUserStoreWidthOut();

  /**
   * 检查权限
   * @param accesses
   */
  function _somePermissions(accesses: string[]) {
    let reg = /pageProject\/project/;

    let pages: any = getCurrentPages(); // 当前页面
    let currentPage: any = pages[pages.length - 1]; // 当前页面
    const isProjectInner = reg.test(currentPage.route);
    const permissions = !isProjectInner
      ? userStore.getPermissions
      : userStore.getProjectPermissions;
    return permissions.some((item: any) => {
      return accesses.includes(item);
    });
  }

  /**
   * 判断是否存在权限
   * 可用于 v-if 显示逻辑
   * */
  function hasPermission(accesses: string[]): boolean {
    if (!accesses || !accesses.length) return true;
    return _somePermissions(accesses);
  }

  return { hasPermission };
}
